package service.Impl;

import dao.*;
import entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.RetreatDao;

import javax.servlet.http.HttpServletRequest;
@Service
public class RetreatDaoImpl implements RetreatDao {
    @Autowired
    public ArauditorDetailsDao arauditorDetailsDao;
    @Autowired
    public dao.retreatDao retreatDao;
    @Autowired
    public RepertoryDao repertoryDao;
    @Autowired
    public ArauditorDao arauditorDao;
    @Autowired
    public dao.userDao userDao;
    @Autowired
    public collectDao collectDao;
    @Autowired
    public detailsDao detailsDao;
    @Override
    public void addretreat(String id, HttpServletRequest request) {

        id = id.substring(0, id.length() - 1);//去掉最后一个字符
        String[] ids = id.split(",");//根据,拆分成数组
        for (int i = 0; i < ids.length; i++) {
           ArauditorDetails arauditorDetails= arauditorDetailsDao.showAr(Integer.parseInt(ids[i]));
            Retreat retreat=new Retreat();
            retreat.setRepertoryId(arauditorDetails.getRepertory().getRepertoryId());
            retreat.setRetreatNum(arauditorDetails.getNums());
            retreat.setRetreatRemark(arauditorDetails.getBeizhu());
            retreat.setSeat(arauditorDetails.getSeat());
            retreat.setTotalPrice(arauditorDetails.getNums()*arauditorDetails.getRepertory().getPresetPrice());
            retreatDao.addretreat(retreat);
           arauditorDetailsDao.tuihuo(Integer.parseInt(ids[i]));
        }
        arauditorDetailsDao.upout();//把备注和退货数量设置为空
    }

    @Override
    public void upretreat(int Reteeatid, int save,HttpServletRequest request) {
        //取出正在登录的管理员所有信息
        Administrator administrator=(Administrator) request.getSession().getAttribute("ad");
        if(save==1){//审核通过
            retreatDao.upretreat(Reteeatid,"拒退",administrator.getAname());
        }else if(save==2){//审核未通过
            retreatDao.upretreat(Reteeatid,"已退",administrator.getAname());
            Retreat retreat=retreatDao.showretreats(Reteeatid);//根据退货编号取出该信息
            repertoryDao.upkucunNum(retreat.getRepertoryId(),retreat.getRetreatNum());//根据退货数量增加到库存信息
            Arauditor arauditor=arauditorDao.showSeatArauditor(retreat.getSeat());//根据单号取出退货用户编号
            User user=userDao.showUser(arauditor.getUser().getUserId());//根据用户编号取出用户信息
            collectDao.upUserprice(user.getUserName(),user.getIphoneNum(),retreat.getTotalPrice());
            Details details=new Details();
            details.setAid(administrator.getAid());
            details.setPaid(-retreat.getTotalPrice());
            details.setAmount(-retreat.getTotalPrice());
            details.setBtype(user.getUserName());
            details.setIphoneNum(user.getIphoneNum());
            details.setSeat(retreat.getSeat());
            details.setSynopsis("商品退货");
            detailsDao.addDetails(details);
        }
    }
}
